package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.domain.Permission;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContextUtil {
    public static final String EMPLOYEE_SESSION_NAME = "EMPLOYEE_IN_SESSION";
    public static final String PERMISSION_SESSION_NAME = "PERMISSION_IN_SESSION";
    public static final String NOTICE_SESSION_NAME = "NOTICE_IN_SESSION";

    /**
     * @return 返回上下文对象中的session对象
     */
    public static HttpSession getSession(){
        ServletRequestAttributes attrs =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    public static Employee getCurrentEmployee(){
        return (Employee) getSession().getAttribute(EMPLOYEE_SESSION_NAME);
    }

    public static void setCurrentEmployee(Employee employee){
        getSession().setAttribute(EMPLOYEE_SESSION_NAME, employee);
    }

    public static List<Permission> getCurrentPermissions(){
        return (List<Permission>) getSession().getAttribute(PERMISSION_SESSION_NAME);
    }

    public static void setCurrentPermissions(List<Permission> permissionList){
        getSession().setAttribute(PERMISSION_SESSION_NAME, permissionList);
    }

    public static List<Notice> getCurrentUnReadNotices(){
        return (List<Notice>) getSession().getAttribute(NOTICE_SESSION_NAME);
    }

    public static void setCurrentUnReadNotices(List<Notice> unReadNotices){
        getSession().setAttribute(NOTICE_SESSION_NAME, unReadNotices);
    }
}
